package com.zwx.bingo.abstraction.endpoint;

import com.zwx.bingo.data.interfaces.IRawService;
import com.zwx.bingo.loggable.annotation.Loggable;
import com.zwx.bingo.stateful.Responses;
import com.zwx.bingo.stateful.body.StatefulBody;
import com.zwx.bingo.stateful.body.impl.SuccessResponseBody;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.io.Serializable;

@FunctionalInterface
public interface FindOneByIdEndpoint<ENTITY extends Serializable, ID extends Serializable> {

    @Autowired
    public abstract IRawService<ENTITY, ID> getRawService();

    /**
     * 使用  @Pretend(value = "**,-payload.items.code") 进行结果排除，不需要此字段在前端展示
     */
    //TODO 添加权限点控制
    @Loggable
    @GetMapping(value = "/{id}", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ApiOperation(value = "Find one by id")
    public default ResponseEntity<StatefulBody<ENTITY>> findOne(@PathVariable ID id) {
        ENTITY entity = getRawService().findOne(id);
        return Responses.ok(SuccessResponseBody.builder()
                .payload(entity).build());
    }
}
